package com.kye.express.business.personal_my_bill.Bill.detail;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.SparseArray;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatActivity;
import com.kye.express.databinding.ActivityBillDetailBinding;
import com.kye.express.utils.ToolbarManager;
import com.kye.express.view.ToastManager;
import com.kye.lib.view.statuslayout.StatusLayout;
import com.kye.lib.view.statuslayout.StatusLayoutType;
import com.kye.model.bean.bill.BillListBean;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

public class BillDetailActivity extends BaseAppCompatActivity implements BillDetailContract.BillDetailView {

    private static final String KEY_POSITION = "KEY_POSITION";
    private static final String KEY_BILL_LIST = "KEY_BILL_LIST";

    private ActivityBillDetailBinding mBinding;
    private int mPosition;
    private List<BillListBean.BillBean> mBeanList;
    private BillDetailContract.BillDetailPresenter mPresenter;
    private SparseArray<BillListBean.BillBean> mBeanSparseArray;

    public static void startActivity(Context context, int position, ArrayList<?> items) {
        context.startActivity(
                new Intent(context, BillDetailActivity.class)
                        .putExtra(KEY_POSITION, position)
                        .putExtra(KEY_BILL_LIST, items)
        );
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_bill_detail);

        final Intent intent = getIntent();
        mPosition = intent.getIntExtra(KEY_POSITION, 0);
        mBeanList = (ArrayList<BillListBean.BillBean>) intent.getSerializableExtra(KEY_BILL_LIST);

        initView();
        initEvent();
        initData();
    }

    private void initView() {
        mBinding.textViewOriginalCost.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
    }

    private void initEvent() {
        ToolbarManager.setToolbar(this, mBinding.toolbar);

        RxView.clicks(mBinding.linearLayoutErrorNet.btErrorNetRetry)
                .compose(new DebounceObservableTransformer<>())
                .compose(bindToLifecycle())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        mBinding.statusLayout.showLoadingView();
                    }
                });

        RxView.clicks(mBinding.linearLayoutErrorServer.btErrorServerRetry)
                .compose(new DebounceObservableTransformer<>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        mBinding.statusLayout.showLoadingView();
                    }
                });
        RxView.clicks(mBinding.linearLayoutEmpty.btEmptyRetry)
                .compose(new DebounceObservableTransformer<>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        mBinding.statusLayout.showLoadingView();
                    }
                });

        mBinding.statusLayout.setOnLoadingListener(new StatusLayout.OnLoadingListener() {
            @Override
            public void onLoading() {
                mPresenter.load(mBeanList.get(mPosition));
            }
        });

        mBinding.statusLayout.setOnStatusChangeListener(new StatusLayout.OnStatusChangeListener() {
            @Override
            public void onStatusChange(StatusLayoutType currentStatus) {
                mBinding.setLoading(currentStatus == StatusLayoutType.LOADING);
            }
        });

        RxView.clicks(mBinding.buttonPrevious)
                .compose(new DebounceObservableTransformer<>())
                .compose(bindToLifecycle())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (mPosition <= 0) {
                            ToastManager.show(BillDetailActivity.this, "已经是最后一条");
                            return;
                        }

                        mPosition--;

                        final BillListBean.BillBean bean = mBeanSparseArray.get(mPosition);
                        if (bean != null) {
                            mBinding.setPosition(mPosition);
                            mBinding.setBean(bean);
                        } else {
                            mBinding.statusLayout.showLoadingView();
                        }
                    }
                });

        RxView.clicks(mBinding.buttonNext)
                .compose(new DebounceObservableTransformer<>())
                .compose(bindToLifecycle())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (mPosition == mBeanList.size() - 1) {
                            finish();
                            return;
                        }

                        mPosition++;

                        final BillListBean.BillBean bean = mBeanSparseArray.get(mPosition);
                        if (bean != null) {
                            mBinding.setPosition(mPosition);
                            mBinding.setBean(bean);
                        } else {
                            mBinding.statusLayout.showLoadingView();
                        }
                    }
                });
    }

    private void initData() {
        mBinding.setCount(mBeanList.size());
        updatePosition();

        mBeanSparseArray = new SparseArray<BillListBean.BillBean>();

        mPresenter = new BillDetailContract.BillDetailPresenter();
        mPresenter.bind(this);

        mBinding.statusLayout.showLoadingView();
    }

    private void updatePosition() {
        mBinding.setPosition(mPosition);
    }

    @Override
    public void onError() {
        mBinding.statusLayout.showErrorNetworkView();
    }

    @Override
    public void onError(String msg) {
        mBinding.statusLayout.showErrorServerView();
    }

    @Override
    public void onLoading() {
        mBinding.statusLayout.showLoadingView();
    }

    @Override
    public void onLoaded() {

    }

    @Override
    public void onLoadedData(BillListBean.BillBean billBean) {
        mBeanSparseArray.append(mPosition, billBean);

        mBinding.statusLayout.showContentView();

        mBinding.setPosition(mPosition);
        mBinding.setBean(billBean);
    }
}
